package com.cpi.cornpp.grinddistributionversion.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.grinddistributionversion.dto.GrindDistributionPrioritiesVersionDTO;
import com.cpi.cornpp.grinddistributionversion.dto.GrindDistributionSettingsVersionDTO;
import com.cpi.cornpp.grinddistributionversion.dto.GrindDistributionVersionDTO;
import com.cpi.cornpp.grinddistributionversion.exception.GrindDistributionVersionException;
import com.cpi.cornpp.grinddistributionversion.view.GrindDistributionVersionPlantGroupView;
import com.cpi.cornpp.grinddistributionversion.view.GrindDistributionVersionView;
import com.cs.common.utils.NumberUtil;

/**
 * Grind Priority Distribution Version Converter
 * @author Edgar Joao
 *
 */
public class GrindDistributionVersionConverter {
    /**
     * 
     * @param dtos
     * @return
     * @throws GrindDistributionVersionException
     */
	public GrindDistributionVersionView convertGrindDistributionVDTOToView(GrindDistributionVersionDTO[] dtos)throws GrindDistributionVersionException{
		GrindDistributionVersionView view = null;
		GrindDistributionSettingsVersionDTO sDTO = (GrindDistributionSettingsVersionDTO) dtos[0];
		GrindDistributionPrioritiesVersionDTO pDTO = (GrindDistributionPrioritiesVersionDTO) dtos[1];		
		try{
			if((pDTO != null) && sDTO != null ){
				view = new GrindDistributionVersionView();
				view.setMehodPeriodVersionId(String.valueOf(sDTO.getMonthPeriodVersionId()));
				view.setPlantVersionId(sDTO.getPlantVersionId());
				view.setCornTypeId(String.valueOf(sDTO.getCornTypeId()));								
				view.setUnloadSettingVersionId(String.valueOf(sDTO.getUnloadSettingVersionId()));				
				view.setUnloadAmount(NumberUtil.convertPrice(sDTO.getUnloadAmount()));
				view.setFixed(sDTO.getFixed());									
				view.setGrindDistributionPriorityVersionId(String.valueOf(pDTO.getGrindDistributionPriorityVersionId()));				
				view.setPriority(String.valueOf(pDTO.getPriority()));				
			}
		}catch( Exception exception ){
			GrindDistributionVersionException grindDistributionVersionException=null;
			exception.printStackTrace(System.out);
			grindDistributionVersionException= new GrindDistributionVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw grindDistributionVersionException;
		}
		return view;
	}
	
	public List convertDTOsToPlantGroupView(List[] grindDistributionDTOs)throws GrindDistributionVersionException{
		List plantGroupViews = null;
		List grindDistributionVersionCornTypeViews = null;
		ListIterator iterator = null;
		GrindDistributionSettingsVersionDTO grindDistributionSettingsVersionDTO = null;
		String plantCodeAnt = null;
		GrindDistributionVersionPlantGroupView grindDistributionVersionPlantGroupView = null;
		GrindDistributionVersionView grindDistributionVersionView = null;
		GrindDistributionVersionDTO [] grindDistributionVersionDTOs = null;
		try{
			grindDistributionVersionDTOs = new GrindDistributionVersionDTO[2];
			plantGroupViews = new ArrayList(0);
			iterator = ((List)grindDistributionDTOs[0]).listIterator();
			while( iterator.hasNext() ){
				grindDistributionSettingsVersionDTO = (GrindDistributionSettingsVersionDTO)iterator.next();
				if( plantCodeAnt == null || !grindDistributionSettingsVersionDTO.getShortName().equals(plantCodeAnt) ){
					if( plantCodeAnt != null ){
						grindDistributionVersionPlantGroupView = new GrindDistributionVersionPlantGroupView();						
						grindDistributionVersionPlantGroupView.setShortName(plantCodeAnt);
						grindDistributionVersionPlantGroupView.setGrindDistributionVersionCornTypeViews(grindDistributionVersionCornTypeViews);
						plantGroupViews.add(grindDistributionVersionPlantGroupView);
					}
					grindDistributionVersionCornTypeViews = new ArrayList(0);
				}
				grindDistributionVersionDTOs[0] = grindDistributionSettingsVersionDTO;
				grindDistributionVersionDTOs[1] = (GrindDistributionVersionDTO)((List)grindDistributionDTOs[1]).get(iterator.previousIndex());
				grindDistributionVersionView = convertGrindDistributionVDTOToView(grindDistributionVersionDTOs);
				grindDistributionVersionCornTypeViews.add(grindDistributionVersionView);
				plantCodeAnt = grindDistributionSettingsVersionDTO.getShortName();
			}
			if( ((List)grindDistributionDTOs[0]).size() > 0 ){
				grindDistributionVersionPlantGroupView = new GrindDistributionVersionPlantGroupView();
				grindDistributionVersionPlantGroupView.setShortName(plantCodeAnt);
				grindDistributionVersionPlantGroupView.setGrindDistributionVersionCornTypeViews(grindDistributionVersionCornTypeViews);
				plantGroupViews.add(grindDistributionVersionPlantGroupView);
			}
		}catch( Exception exception ){
			GrindDistributionVersionException grindDistributionVersionException=null;
			exception.printStackTrace(System.out);
			grindDistributionVersionException= new GrindDistributionVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw grindDistributionVersionException;
		}
		return plantGroupViews;
	}

	public GrindDistributionPrioritiesVersionDTO convertViewToPrioritiesDTO(int plantVersionId, GrindDistributionVersionView grindDistributionVersionView)throws GrindDistributionVersionException {
		GrindDistributionPrioritiesVersionDTO grindDistributionPrioritiesVersionDTO = null;
		try{
			grindDistributionPrioritiesVersionDTO = new GrindDistributionPrioritiesVersionDTO();
			grindDistributionPrioritiesVersionDTO.setCornTypeId(new Integer(grindDistributionVersionView.getCornTypeId()));
			grindDistributionPrioritiesVersionDTO.setPlantVersionId(new Integer(plantVersionId));
			if( !StringUtils.isEmpty(grindDistributionVersionView.getGrindDistributionPriorityVersionId()) ){
				grindDistributionPrioritiesVersionDTO.setGrindDistributionPriorityVersionId(new Integer(grindDistributionVersionView.getGrindDistributionPriorityVersionId()));
			}else{
				grindDistributionPrioritiesVersionDTO.setGrindDistributionPriorityVersionId(new Integer(0));
			}
			if( !StringUtils.isEmpty(grindDistributionVersionView.getPriority()) ){
				grindDistributionPrioritiesVersionDTO.setPriority(new Integer(grindDistributionVersionView.getPriority()));
			}else{
				grindDistributionPrioritiesVersionDTO.setPriority(new Integer(0));
			}
		}catch( Exception exception ){
			GrindDistributionVersionException grindDistributionVersionException=null;
			exception.printStackTrace(System.out);
			grindDistributionVersionException= new GrindDistributionVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw grindDistributionVersionException;
		}
		return grindDistributionPrioritiesVersionDTO;
	}

	public GrindDistributionSettingsVersionDTO convertViewToSettingsDTO(int plantVersionId, GrindDistributionVersionView grindDistributionVersionView)throws GrindDistributionVersionException {
		GrindDistributionSettingsVersionDTO grindDistributionSettingsVersionDTO = null;
		try{
			grindDistributionSettingsVersionDTO = new GrindDistributionSettingsVersionDTO();
			grindDistributionSettingsVersionDTO.setMonthPeriodVersionId(new Integer(grindDistributionVersionView.getMonthPeriodVersionId()));
			grindDistributionSettingsVersionDTO.setCornTypeId(new Integer(grindDistributionVersionView.getCornTypeId()));
			grindDistributionSettingsVersionDTO.setPlantVersionId(new Integer(plantVersionId));
			if( !StringUtils.isEmpty(grindDistributionVersionView.getGrindDistributionPriorityVersionId()) ){
				grindDistributionSettingsVersionDTO.setUnloadSettingVersionId(new Integer(grindDistributionVersionView.getGrindDistributionPriorityVersionId()));
			}else{
				grindDistributionSettingsVersionDTO.setUnloadSettingVersionId(new Integer(0));
			}
			if( !StringUtils.isEmpty(grindDistributionVersionView.getUnloadAmount()) ){
				grindDistributionSettingsVersionDTO.setUnloadAmount(Double.parseDouble(grindDistributionVersionView.getUnloadAmount().replaceAll(",", StringUtils.EMPTY)));
			}else{
				grindDistributionSettingsVersionDTO.setUnloadAmount(0);
			}
			if( grindDistributionVersionView.getFixed() != null ){
				grindDistributionSettingsVersionDTO.setFixed(grindDistributionVersionView.getNewFixed());
			}else{
				grindDistributionSettingsVersionDTO.setFixed(new Integer(0));
			}
		}catch( Exception exception ){
			GrindDistributionVersionException grindDistributionVersionException=null;
			exception.printStackTrace(System.out);
			grindDistributionVersionException= new GrindDistributionVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw grindDistributionVersionException;
		}
		return grindDistributionSettingsVersionDTO;
	}
}
